package com.easy.component.loadingpage;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.easy.component.R;
import com.easy.component.ibase.EasyBaseActivity;

/**
 * 描述: {加载页面过渡页}
 * 作者: jack-song
 * 创建日期: 2017/11/9 14:48
 * 修改日期: 2017/11/9
 * 版本: {版本}
 */
public class LoadingPageView extends LinearLayout {
    /**
     * 上下文
     */
    private EasyBaseActivity mContext;
    /**
     * 转圈
     */
    private View loadingLayout;
    /**
     * 网络加载失败
     */
    private View loadingErrorLayout;
    /**
     * 暂无数据
     */
    private View loadingNotLayout;

    public LoadingPageView(Context context) {
        super(context);
        init(context);
    }

    public LoadingPageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LoadingPageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = (EasyBaseActivity) context;
        loadingLayout = LayoutInflater.from(context).inflate(R.layout.easy_loading_page, null);
        loadingErrorLayout = LayoutInflater.from(context).inflate(R.layout.easy_loading_error_page, null);
        loadingNotLayout = LayoutInflater.from(context).inflate(R.layout.easy_loading_no_data_page, null);
        this.addView(loadingLayout);
        this.addView(loadingErrorLayout);
        this.addView(loadingNotLayout);
        this.setVisibility(VISIBLE);
        loadingLayout.setVisibility(GONE);
        loadingErrorLayout.setVisibility(GONE);
        loadingNotLayout.setVisibility(GONE);
    }

    /**
     * 显示过度页面弹出框（按返回键关闭页面）
     */
    public void hideAlertLoadingPage() {
        this.setVisibility(GONE);
        mContext.hideLoading();
        loadingLayout.setVisibility(GONE);
        loadingErrorLayout.setVisibility(GONE);
        loadingNotLayout.setVisibility(GONE);
    }

    /**
     * 显示过度页面（按返回键关闭页面）
     */
    public void showAlertLoadingPage() {
        mContext.showLoadingCloseActivity();
        this.setVisibility(VISIBLE);
        loadingErrorLayout.setVisibility(GONE);
        loadingNotLayout.setVisibility(GONE);
    }

    /**
     * 显示过度页面
     */
    public void showLoadingPage() {
        this.setVisibility(VISIBLE);
        loadingLayout.setVisibility(VISIBLE);
        loadingErrorLayout.setVisibility(GONE);
        loadingNotLayout.setVisibility(GONE);
    }

    /**
     * 关闭过度页面
     */
    public void hideLoadingPage() {
        this.setVisibility(GONE);
        loadingLayout.setVisibility(GONE);
        loadingErrorLayout.setVisibility(GONE);
        loadingNotLayout.setVisibility(GONE);
    }

    /**
     * 显示暂无数据
     */
    public void showLoadingNoteData() {
        this.setVisibility(VISIBLE);
        loadingLayout.setVisibility(GONE);
        loadingErrorLayout.setVisibility(GONE);
        loadingNotLayout.setVisibility(VISIBLE);
    }

    /**
     * 显示加载失败页面
     */
    public void showLoadingPageError() {
        this.setVisibility(VISIBLE);
        loadingLayout.setVisibility(GONE);
        loadingErrorLayout.setVisibility(VISIBLE);
        loadingNotLayout.setVisibility(GONE);
    }

    /**
     * 重试按钮
     */
    public void onRetryListener(final RetryOnClickListener retryListener) {
        loadingErrorLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                retryListener.onRetry(view);
            }
        });
        loadingNotLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                retryListener.onRetry(view);
            }
        });
    }

    public interface RetryOnClickListener {
        void onRetry(View view);
    }

}
